﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BarcodeDelay.Transaction
{
    public class DbManager
    {
        private String dbConnStrCamera;
        private DbOperatorFactory cameraDbFactory;
        private String dbCameraProvider;




        public const String PARAMETER_OCCUPY_PREFIX = "?";
        public const String TABLE_PACKET_INFO = "tb_packet_info";
        public const String TABLE_USER_INFO = "tb_user_info";
        public const String TABLE_BILL_INFO = "tb_bill_info";
        public const String TABLE_SORT_RESULT = "tt_sto_packet_off";
        public const String TABLE_PACKAGE_SLOT = "tb_package_slot";
        public const String TABLE_PACKAGE_RESULT = "tt_sto_package_slot";
        public const String TABLE_STATICS_RESULT = "tb_static_by_day";

        public const String TABLE_SORT_PLAN = "tb_sort_plan_out";

        private String dbConnStrLocal;
        private DbOperatorFactory localDbFactory;
        private String dbLocalProvider;

        #region 单例构造
        /// <summary>
        /// 单例构造
        /// </summary>
        private static DbManager instance;
        public static DbManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new DbManager();
                }
                return instance;
            }
        }

        private DbManager()
        {

        }
        #endregion

        public void Init()
        {
            try
            {
                StreamWriter swb = new StreamWriter(System.Environment.CurrentDirectory + "\\task.txt", true);
               

                this.dbCameraProvider = "MySql.Data.MySqlClient";
                this.dbConnStrCamera = StoParams.Instance.db_connstr;
                this.cameraDbFactory = new DbOperatorFactory();
                this.cameraDbFactory.Init(dbConnStrCamera, dbCameraProvider, "camera");
                swb.WriteLine(System.DateTime.Now + "Init db connstr：" + StoParams.Instance.db_connstr);
                swb.WriteLine(System.DateTime.Now + "Init db end" );
                swb.Close();

            }
            catch (System.Exception ex)
            {
                string dir = System.Environment.CurrentDirectory;
                StreamWriter swb = new StreamWriter(dir + "\\task.txt", true);
                swb.WriteLine(System.DateTime.Now + "Init error：" + ex.ToString());
                swb.Close();
                //Logger.LogError("DbManager.Init", ex.Message);
            }
        }

        public void ReadConfguration()
        {
            string methodStr = "DbManager.ReadConfguration";
            try
            {
                this.dbCameraProvider = "MySql.Data.MySqlClient";
                this.dbConnStrCamera = "Server=127.0.0.1;Database=test;Uid=root;Pwd=Simba123;Charset=utf8";

            }
            catch (System.Exception ex)
            {
                //Logger.LogError(methodStr, "读取配置文件失败！" + ex.Message);
            }
        }

        public void ShutDown()
        {
            if (this.cameraDbFactory != null)
            {
                this.cameraDbFactory.ShutDown();
            }
        }

        public DbOperatorFactory CameraDbFactory
        {
            get { return this.cameraDbFactory; }
        }

        public DbOperatorFactory LocalDbFactory
        {
            get { return this.localDbFactory; }
        }



        //////////////////////////////////////////////////////////////////////////End Of Class
    }
}
